package com.cashmate.hibernate.dao;

import com.cashmate.hibernate.pojo.UsertypeEmployee;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;

public class UsertypeEmployeeDAO extends DAO {

    public UsertypeEmployeeDAO() {
        super();
    }

    //BASIC DAO METHODS
    @SuppressWarnings("unchecked")
    public List<UsertypeEmployee> getAll() throws Exception {
        List<UsertypeEmployee> a = (List<UsertypeEmployee>) getSession().createQuery("from UsertypeEmployee a").setCacheable(true).list();
       // flush();
        return a;
    }

    @SuppressWarnings("unchecked")
    public UsertypeEmployee get(int id) throws Exception {
        UsertypeEmployee a = (UsertypeEmployee) getSession().createQuery("from UsertypeEmployee a where a.usertypeEmployeeid=:id").setParameter("id", id).setCacheable(true).uniqueResult();
        flush();
        return a;
    }

    @SuppressWarnings("unchecked")
    public void update(UsertypeEmployee a) throws Exception {
        try {
            begin();
            getSession().update(a);
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public void insert(UsertypeEmployee a) throws Exception {
        try {
            begin();
            getSession().save(a);
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public void delete(int id) throws Exception {
        try {
            begin();
            getSession().delete(get(id));
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public List<UsertypeEmployee> findByCriteria(DetachedCriteria dc, int from, int size) throws Exception {
        Criteria criteria = dc.getExecutableCriteria(getSession());
        criteria.setFirstResult(from);
        criteria.setMaxResults(size);
        criteria.setCacheable(true);
        List<UsertypeEmployee> list = criteria.list();
        flush();
        return list;
    }

    @SuppressWarnings("unchecked")
    public int countByCriteria(DetachedCriteria dc) throws Exception {
        Criteria criteria = dc.getExecutableCriteria(getSession());
        criteria.setCacheable(true);
        criteria.setProjection(Projections.rowCount());
        int count = ((Integer) criteria.list().get(0)).intValue();
        flush();
        return count;
    }
    
    //OTHER METHODS

    @SuppressWarnings("unchecked")
    public int nextMatrixNumber() throws Exception {
        DetachedCriteria dc = DetachedCriteria.forClass(UsertypeEmployee.class);
        Criteria criteria = dc.getExecutableCriteria(getSession());
        criteria.setProjection(Projections.max("matrixNo"));
        return ((Integer) criteria.list().get(0)).intValue() + 1;
    }

    @SuppressWarnings("unchecked")
    public UsertypeEmployee getByMatrix(int matrixNo) throws Exception {
        UsertypeEmployee u = (UsertypeEmployee) getSession().createQuery("from UsertypeEmployee ut where ut.matrixNo=:matrix").setParameter("matrix", matrixNo).setMaxResults(1).setCacheable(true).uniqueResult();
        flush();
        return u;
    }

    @SuppressWarnings("unchecked")
    public List<UsertypeEmployee> getByUsertype(int usertypeid) throws Exception {
        List<UsertypeEmployee> list = (List<UsertypeEmployee>) getSession().createQuery("from UsertypeEmployee ue where ue.usertypes.usertypeid=:id and ue.employees.isactive=:true").setParameter("id", usertypeid).setParameter("true", true).setCacheable(true).list();
        flush();
        return list;
    }

    @SuppressWarnings("unchecked")
    public void deleteByMatrix(int matrixNo) throws Exception {
        try {
            begin();
            getSession().createQuery("delete from UsertypeEmployee ut where ut.matrixNo=:matrix").setParameter("matrix", matrixNo).executeUpdate();
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }
}
